package com.mozai.shopping.controller;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.mozai.shopping.pojo.Activity;
import com.mozai.shopping.pojo.PageResp;
import com.mozai.shopping.pojo.ProductType;
import com.mozai.shopping.pojo.base.BaseResponse;
import com.mozai.shopping.pojo.base.ErrorMessage;
import com.mozai.shopping.pojo.http.*;
import com.mozai.shopping.service.ActivityService;
import com.mozai.shopping.utils.bean.CommonQueryBean;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    /**
     * 活动新增
     * @param activityAddReq
     * @return
     */
    @PostMapping("/addActivity")
    public BaseResponse addActivity(@Valid @RequestBody ActivityAddReq activityAddReq){

        try {
            log.info("新增活动：{}",activityAddReq);
            Activity activity=new Activity();
            BeanUtils.copyProperties(activityAddReq,activity);
            activityService.insertActivity(activity);
        } catch (BeansException e) {
            log.info("活动新增发生异常：{}",e);
        }
        return BaseResponse.getOK();
    }

    /**
     * 删除活动
     * @param activityDelReq
     * @return
     */
    @PostMapping("/delActivity")
    public BaseResponse deleteActivity(@Valid @RequestBody ActivityDelReq activityDelReq){

        try {
            Activity activity = new Activity();
            BeanUtils.copyProperties(activityDelReq,activity);
            int del = activityService.deleteActivity(activity);
            if (del == 0){
                return BaseResponse.error(ErrorMessage.ACTIVITY_DEL);
            }
        } catch (BeansException e) {
            log.info("活动删除发生异常：{}",e);
        }
        return BaseResponse.getOK();
    }

    /**
     * 修改活动
     * @param activityUpdateReq
     * @return
     */
    @RequestMapping(value = "/modifyActivity", method = RequestMethod.POST)
    public BaseResponse modifyActivity(@Valid @RequestBody ActivityUpdateReq activityUpdateReq){
        try {
            log.info("活动信息修改：{}",activityUpdateReq);
            Activity activity = new Activity();
            BeanUtils.copyProperties(activityUpdateReq,activity);
            if (activityService.modifyActivity(activity) == 0){
                return BaseResponse.error(ErrorMessage.ACTIVITY_DEL);
            }
        }catch(Exception e){
            log.error("活动信息修改发生异常:{}",e);
        }
        return BaseResponse.getOK();
    }


    /**
     * 分页查询
     * @param activityPageReq
     * @return
     */
    @RequestMapping(value = "/queryActivityList", method = RequestMethod.POST)
    public BaseResponse queryActivityList(@Valid @RequestBody ActivityPageReq activityPageReq){
        PageResp<Activity> activityPageResp = new PageResp<>();
        try {
            log.info("活动列表查询：{}", activityPageReq);
            Activity activity = new Activity();
            CommonQueryBean commonQueryBean = new CommonQueryBean();
            BeanUtils.copyProperties(activityPageReq,activity);
            BeanUtils.copyProperties(activityPageReq,commonQueryBean);
            activityPageResp = activityService.queryActivityList(activity, commonQueryBean);
        }catch(Exception e){
            log.error("活动列表查询发生异常：{}",e);
        }
        return BaseResponse.ok(activityPageResp);
    }


    /**
     * 查询单个活动
     * @param activityQueryRep
     * @return
     */
    @PostMapping("/queryActivity")
    public BaseResponse queryActivity(@Valid @RequestBody ActivityQueryRep activityQueryRep){
        Activity activity = new Activity();
        try {
            activity = activityService.queryActivity(activityQueryRep);
        } catch (BeansException e) {
            log.error("查询单个活动出现异常：{}",e);
        }
        return BaseResponse.OK(activity);
    }

}
